/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Event;
import ticketmaster.dataobject.EventView;
import ticketmaster.util.Converter;

/**
 *
 * @author Wei Liu
 */
public class EventViewDAO extends BaseEntityDAO
{

    /**
     * Get all events that match the supplied criteria
     * @param event supplied criteria
     * @return a collection of Event Data Objects
     */
    public Collection<EventView> getEventsDOByName(EventView event)
    {
        String sql = "select * from eventView where eventName like \'%" + event.getName() + "%\' and beginDateTime > '" + event.getBeginDateTime().toString() + "' and endDateTime < '" + event.getEndDateTime().toString() + "' and cancelled = " + Converter.BooleanToInt(event.isCancelled());

        RowSet crs = query(sql);
        Collection<EventView> events = new ArrayList<EventView>();
        try
        {
            while (crs.next())
            {
                EventView eventObject = (EventView) this.getObject(crs, event);
                if(!eventObject.isCancelled())
                {
                    EventView eventDO = new EventView();
                    eventDO.setEventID(eventObject.getEventID());
                    eventDO.setVenueID(eventObject.getVenueID());
                    eventDO.setName(eventObject.getName());
                    eventDO.setDescription(eventObject.getDescription());
                    eventDO.setBeginDateTime(eventObject.getBeginDateTime());
                    eventDO.setEndDateTime(eventObject.getEndDateTime());
                    eventDO.setVenueName(eventObject.getVenueName());
                    eventDO.setLocation(eventObject.getLocation());
                    eventDO.setPrice(eventObject.getPrice());
                    events.add(eventDO);
                }
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return events;
    }

    /**
     * Get all events that match the supplied criteria
     * @param event supplied criteria
     * @return a collection of Event Data Objects
     */
    public Collection<EventView> getEventsDO(EventView event)
    {
        int counter = 0;
        StringBuilder sb = new StringBuilder();
        sb.append("select * from eventView where ");
        if (event.getName() != null && !event.getName().isEmpty() && !event.getName().equals(""))
        {
            sb.append("eventName like \'%" + event.getName() + "%\'");
            counter++;
        }

        if (event.getVenueName() != null)
        {
            if (!event.getVenueName().isEmpty())
            {
                if (counter > 0)
                {
                    sb.append(" or ");
                }
                sb.append("VenueName like \'%" + event.getVenueName() + "%\'");
                counter++;
            }
        }

        if (event.getLocation() != null)
        {
            if (!event.getLocation().isEmpty())
            {
                if (counter > 0)
                {
                    sb.append(" or ");
                }
                sb.append("location like \'%" + event.getLocation() + "%\'");
                counter++;
            }
        }

        if (event.getKeywords() != null)
        {
            if (!event.getKeywords().isEmpty())
            {
                if (counter > 0)
                {
                    sb.append(" or ");
                }
                sb.append("keywords like \'%" + event.getKeywords() + "%\'");
                counter++;
            }
        }

        if(counter>0)
        {
            sb.append(" and ");
        }
        
        sb.append("(beginDateTime > '" + event.getBeginDateTime().toString() + "' and endDateTime < '" + event.getEndDateTime().toString() + "')");
        sb.append(" and cancelled = ");
        sb.append(Converter.BooleanToInt(event.isCancelled()));
        
        String sql = sb.toString();//"select * from eventView where eventName like \'%" + event.getName() + "%\' or (beginDateTime > '" + event.getBeginDateTime().toString() + "' and endDateTime < '" + event.getEndDateTime().toString() + "') or VenueName like \'%" + event.getVenueName() + "%\' or keywords like \'%" + event.getKeywords() + "%\' or location like like \'%" + event.getLocation() + "%\'";

        RowSet crs = query(sql);
        Collection<EventView> events = new ArrayList<EventView>();
        try
        {
            while (crs.next())
            {
                EventView eventObject = (EventView) this.getObject(crs, event);
                EventView eventDO = new EventView();
                eventDO.setEventID(eventObject.getEventID());
                eventDO.setVenueID(eventObject.getVenueID());
                eventDO.setName(eventObject.getName());
                eventDO.setDescription(eventObject.getDescription());
                eventDO.setBeginDateTime(eventObject.getBeginDateTime());
                eventDO.setEndDateTime(eventObject.getEndDateTime());
                eventDO.setVenueName(eventObject.getVenueName());
                eventDO.setLocation(eventObject.getLocation());
                eventDO.setPrice(eventObject.getPrice());
                events.add(eventDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return events;
    }
}
